package ictbk.ttmk51.grapro.app.androidexplorer;

import ictbk.ttmk51.grapro.app.R;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class ReadTextFile extends Activity {
	
	private Intent intent;
	private File txtFile;
	private String pathTXTFile = null;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.read_text_file_screen);
		TextView tvContent = (TextView)findViewById(R.id.texview);
//		TextView tvFileName = (TextView)findViewById(R.id.txtFileName);
		
//		
		//call intent
		intent = getIntent();
		pathTXTFile = intent.getStringExtra("pathFile");
		txtFile = new File(pathTXTFile);
		
		setTitle("TXT Viewer - " + txtFile.getName());
//		tvFileName.setText(txtFile.getName());
		
		Log.i("co chay duoc den day khong?", "OK");
		
		StringBuffer strBuilder = new StringBuffer();
		
		try{
			BufferedReader bufReader = new BufferedReader(new FileReader(txtFile));
			String line = null;
			
			//repeat until all lines is read
			while((line =  bufReader.readLine()) != null){
				strBuilder.append(line).append(System.getProperty("line.separator"));
//				txtFileView.setText(strBuilder.toString());
			}
			bufReader.close();
		}catch(IOException e){
			Log.i("Chet roai!", "hixhixhix");
			Toast.makeText(ReadTextFile.this, "Don't can open file!", Toast.LENGTH_SHORT).show();
		}
		
		System.out.print(strBuilder.toString());
		//View TXT File
		tvContent.setText(strBuilder.toString());
	}
}
